'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟商城
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var unionGroupModel = require('../../../../models/unionGroupModel');
var PlayerManager = require('../../../../libs/playerManager');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionShopConf = require('../../../../config/unionShopConf.js');
var unionGroupService = require('../../../../services/unionGroupService');
var onlineService = require('../../../../services/onlineService.js');
var dataApi = require('../../../../libs/dataApi.js');
var roleService = require('../../../../services/roleService');

/**
 * 联盟商城
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var refresh_shop = msg.refresh_shop;

    var union_msg;
    var role_model;
    var union_id;
    var union_shop_list = [];
    var buy_num_list = [];
    var union_task_model;
    var union_group_model;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'task', 'achieve', 'daily_task', 'unionTask', 'accrued'],
                    cb
                );
            },
            function (cb) {
                role_model = player_manager.role;
                union_task_model = player_manager.unionTask;

                union_id = role_model.getUnionId();

                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, cb);
            },
            function (model, cb) {
                union_group_model = model;

                if (refresh_shop !== 1) {
                    return cb();
                }

                // 刷新商城
                var next_refresh_times = union_task_model.refresh_shop_times + 1;
                var refresh_conf = dataApi.shop_refresh.findById(next_refresh_times);
                if (!refresh_conf) {
                    return next(null, {
                        code: Code.SHOP_INVALID_REFRESH_TIMES,
                    });
                }

                // 扣除钻石
                var sub_diamond = refresh_conf.union_shop;
                if (!roleService.subDiamond(player_manager, moduleType.MDL_UNION, operationType.OPR_UNION_REFRESH_SHOP, sub_diamond)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 刷新商城
                if (!union_task_model.manulRefreshShop()) {
                    return next(null, {
                        code: Code.SHOP_NO_REFRESH_TIMES,
                    });
                }

                player_manager.save(cb);
            },
            function (cb) {
                var union_lv = union_group_model.lv;
                union_shop_list = unionShopConf.getConf(union_lv);
                var shop_list = union_task_model.buy_num_list;

                _.each(shop_list, function (shop_num, shop_id) {
                    buy_num_list.push({
                        shop_id: +shop_id,
                        shop_num: +shop_num
                    });
                });
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {
                    union_shop_list: union_shop_list,
                    buy_num_list: buy_num_list
                }
            });
        }
    );
};
